#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include<dirent.h>
#include <stdlib.h>


void get_files(char *p, char **image_paths, int *img_cnt)
{
    DIR *dir;
    char base_path[512];
    memset(base_path, 0, sizeof base_path);
    int offset = 0;
    if(p && p[0] == '.'){
        getcwd(base_path, sizeof(base_path));   //如果为工作目录
        offset ++;
    }
    strcat(base_path, p + offset);
    struct dirent *ptr;   //定义目录目录项指针
    if ((dir=opendir(base_path)) == NULL)
    {
        printf("Open dir: %s Error...\n", base_path);
        exit(1);
    }
   while ((ptr=readdir(dir)) != NULL)   //不断的读取目录里面的选项
    {
        if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)    //跳过./.. 目录
            continue;
        if(ptr->d_type == 8)   //文件的格式   
        {
            printf("d_name:%s/%s\n",base_path,ptr->d_name);
            
            if(strcmp(ptr->d_name + strlen(ptr->d_name) - 3, "jpg") && strcmp(ptr->d_name + strlen(ptr->d_name) - 3, "png")) continue;
            char *tmp;
            tmp = (char *)malloc((strlen(base_path) + strlen(ptr -> d_name) + 5) * sizeof (char *));
            memset(tmp, 0, sizeof tmp);
            sprintf(tmp, "%s%c%s", base_path, '/', ptr->d_name);
            image_paths[(*img_cnt) ++] = tmp;
        }
    }
    closedir(dir);
   // return image_paths;
}

int main( int argc, char** argv )
{
    char *image_paths[5005];
    int img_cnt = 0;
    char* filename="./testfile";
    get_files(filename, image_paths, &img_cnt);
   //printf("%s,%d\n",image_paths,img_cnt);
   int i=0;
   for(i=0;i<img_cnt;i++) printf("%s\n",image_paths[i]);
    return 0;
}